import { createUser } from "../../utils/user";
const App = getApp()
Page({
  data: {
    user: {},
    canGetUser: false,
  },
  //获取用户资料
  getUserProfile(e) {
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
        this.setData({
          user: res.userInfo,
          canGetUser: true
        })
        wx.setStorageSync('user', res.userInfo)
        wx.setStorageSync('canGetUser', true)
        createUser(res.userInfo, res => {
          wx.setStorageSync('token', res.data.token)
        })
      }
    })
  },
  //切换角色
  changeUser() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
    }
  },
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({ selected: 2 })
    }
    const canGetUser = wx.getStorageSync("canGetUser");
    const user = wx.getStorageSync('user')
    if (
      canGetUser
    ) {
      this.setData({ canGetUser: true, user })
    } else {
      this.getUserProfile()
    }
  }
})
